package com.lagou.module4.servlet;

import com.lagou.module4.model.Student;
import com.lagou.module4.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddServlet",urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int ID = Integer.parseInt(request.getParameter("ID"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birth_date = request.getParameter("birth_date");
        String email = request.getParameter("email");
        String comment = request.getParameter("comment");
        StudentService studentService = new StudentService();
        int result = studentService.addStudentService(new Student(ID, name, sex, birth_date, email, comment));
        if (result==1){
            System.out.println("添加成功");
            response.setContentType("text/html;charset=utf-8");
            request.getSession().setAttribute("type","添加用户成功");
            response.sendRedirect("main.jsp");
        }else {
            System.out.println("添加失败");
            response.setContentType("text/html;charset=utf-8");
            request.getSession().setAttribute("type","添加用户失败");
            response.sendRedirect("main.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
